//
//  VisitorLoginView.swift
//  Zayl--weibo
//
//  Created by 张玉 on 15/9/7.
//  Copyright © 2015年 ZY. All rights reserved.
//

import UIKit
//设置代理
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //访客视图将要登录
    func visitorLoginViewWillLogin()
    //访客视图将要注册
    func visitorLoginViewWillRegister()
}

class VisitorLoginView: UIView {
    //设置代理
    weak var delegata: VisitorLoginViewDelegate?
    
    func setupInfo(isHome:Bool, imageName:String, message:String) {
        messageLabel.text = message
        iconView.image = UIImage(named: imageName)
        homeIconView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
     
        
    }
     /// 开始动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 *  M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
     
    }
    
    //设置按钮监听事件
    func clickregisterButton () {
        delegata?.visitorLoginViewWillRegister()
    }
    func clickloginButton () {
        delegata?.visitorLoginViewWillLogin()
    }
    
    //设置界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
        setupUI()
    }
    
    
    //设置界面元素
    private func setupUI() {
        //设置ViewControll的背景颜色 默认是透明的 颜色用测色计得来
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
     
        
        //自动布局
        //1:图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
      addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        //房子图标
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem:iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //描述文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy:  NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 250))
        
        //注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //  登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //遮罩视图
      
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-50)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView, "regButton": registerButton]))

        
        
    }

    //MARK - 懒加载控件
    //图标
    private lazy var  iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        
    }()
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        }()
    //小房子
    private lazy var homeIconView:UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        
    }()
    //描述文字
    private lazy var messageLabel:UILabel = {
       let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜";
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 2
        return label
    }()
    //注册按钮
    private lazy var registerButton:UIButton = {
       let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.purpleColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickregisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    //登录按钮
    private lazy var loginButton:UIButton = {
       let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal
        )
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.purpleColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickloginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    
}
